﻿using System;
using System.Threading;

using Sce.Psm;

namespace CDude.Vita
{
    internal partial class VitaCOM
    {
        private static PsmDeviceConnection connection;
        private static short deviceid = 8;

        private static void com_Connect(string id)
        {
            if (deviceid != 8)
                com_Disconnect(true);

            PsmDevice[] devices = PsmDevice.ListDevices();
            short connectionid = 0;

            try
            {
                connectionid = Convert.ToInt16(id);
            }
            catch
            {
                Console.WriteLine(DEVICE_NOT_VALID);
                return;
            }

            if (!IsValidDevice(connectionid)) return;
            if (!devices[connectionid].Online)
            {
                Console.WriteLine(DEVICE_OFFLINE);
                return;
            }

            connection = devices[connectionid].Connect();
            deviceid = connectionid;
            Console.WriteLine("Connected to device {0}.", connectionid);
        }

        private static void com_Details(string id)
        {
            PsmDevice[] devices = PsmDevice.ListDevices();
            short connectionid = 0;

            try
            {
                connectionid = Convert.ToInt16(id);
            }
            catch
            {
                Console.WriteLine(DEVICE_NOT_VALID);
                return;
            }

            if (!IsValidDevice(connectionid)) return;

            Console.WriteLine("Online: {0}", devices[connectionid].Online);
            Console.WriteLine("GUID: {0}", devices[connectionid].Guid);
            Console.WriteLine("Name: {0}", devices[connectionid].Name);
            Console.WriteLine("Type: {0}", devices[connectionid].Type);
        }

        private static void com_Disconnect(bool echo)
        {
            if (deviceid != 8)
            {
                try
                {
                    connection.Disconnect();
                }
                catch { }
                if (echo) Console.WriteLine("Disconnected from device {0}.", deviceid);
                deviceid = 8;
            }
            else if (echo) Console.WriteLine(NOT_CONNECTED);
        }

        private static void com_Exit()
        {
            Console.WriteLine("Leaving VitaCOM...");
            Environment.Exit(0);
        }

        private static void com_Kill()
        {
            if (!IsConnected()) return;
            if (!IsValidDevice(deviceid)) return;
            connection.KillRunningApps();

            Console.WriteLine("Killed the PSM app.");
        }

        private static void com_Launch(string appname, bool debug, bool profile, bool keepnet, string arguments, bool echo)
        {
            if (!IsConnected()) return;
            if (!IsValidDevice(deviceid)) return;

            connection.LaunchApp(appname, debug, profile, keepnet, arguments);

            if (echo)
            {
                keepnet = true;
                PsmDevice.SetConsoleCallback(new PsmDeviceConsoleCallback(Console.Out.WriteLine));
            }
            else
                PsmDevice.SetConsoleCallback(null);

            Console.WriteLine("Launched {0}.", appname);

            while (IsConnected())
                Thread.Sleep(250);

            Console.Write("\nVITACOM> ");
        }

        private static void com_List()
        {
            PsmDevice[] devices = PsmDevice.ListDevices();
            if (devices.Length < 1)
            {
                Console.WriteLine("There are no devices to show.");
                return;
            }

            Console.WriteLine("  Device #  Status   GUID                                  Type   \n" +
                              "  --------  -------  ------------------------------------  -------");

            for (short i = 0; i < devices.Length; i++)
            {
                string status;
                char prechar = ' ';

                if (deviceid == i) prechar = '*';
                if (PsmDevice.ListDevices()[i].Online) status = Tabs("Online", 7);
                else status = Tabs("Offline", 7);

                Console.WriteLine("{4} Device {0}  {1}  {2}  {3}\n", i, status, devices[i].Guid, devices[i].Type, prechar);
            }
        }

        private static void com_Pack(string inputfolder, string outputfile)
        {
            PsmDevice.CreatePackage(outputfile, inputfolder);
            Console.WriteLine("Packed {0}.", outputfile);
        }

        private static void com_Uninstall(string appname)
        {
            if (!IsConnected()) return;
            if (!IsValidDevice(deviceid)) return;
            connection.UninstallApp(appname);

            Console.WriteLine("Uninstalled {0}.", appname);
        }

        private static void com_Upload(string packagepath, string outputappname)
        {
            if (!IsConnected()) return;
            if (!IsValidDevice(deviceid)) return;
            connection.InstallApp(packagepath, outputappname);

            Console.WriteLine("Installed {0}.", outputappname);
        }
    }
}